<?php

namespace app\api\controller;

use support\Request;
use Tinywan\Jwt\JwtToken;

class IndexController extends BaseController
{
    /**
     * @title: 测试接口
     * @url: /api/index/index
     * @date: 2025/4/2
     * @time: 10:36
     * @param Request $request
     * @return \support\Response
     */
    public function index(Request $request)
    {
        return $this->success();
    }

    /**
     * 生成令牌接口
     */
    public function token(Request $request)
    {
        $user = [
            'id'  => 2022, // 这里必须是一个全局抽象唯一id
            'name'  => 'Tinywan',
            'email' => 'Tinywan@163.com'
        ];
        $token = JwtToken::generateToken($user);
        //var_dump(json_encode($token));

        return $this->success('ok', ['token' => $token]);
    }

    /**
     * 获取当前UID
     */
    public function getUid(Request $request)
    {
        // 手动捕获异常
//        try {
//            $uid = JwtToken::getCurrentId();
//            // var_dump($uid); // 可以考虑移除或替换为日志记录
//
//            return $this->success('ok', ['uid' => $uid]);
//        } catch (\Exception $e) {
//            // 捕获异常并返回异常信息
//            return $this->error('error', ['error_message' => $e->getMessage()]);
//        }

        $uid = JwtToken::getCurrentId();
        // var_dump($uid); // 可以考虑移除或替换为日志记录

        return $this->success('ok', ['uid' => $uid]);
    }

    /**
     * 获取所有扩展字段
     */
    public function getExtend(Request $request)
    {
        $claims = JwtToken::getExtend();

        return $this->success('ok', ['extend' => $claims]);
    }


    /**
     * 获取自定义字段
     */
    public function getCustom(Request $request)
    {
        $claims = JwtToken::getExtendVal('email');

        return $this->success('ok', ['email' => $claims]);
    }

    /**
     * 刷新令牌（通过刷新令牌获取访问令牌）
     */
    public function refreshToken(Request $request)
    {
        $token = JwtToken::refreshToken();
        return $this->success('ok', ['token' => $token]);
    }

    /**
     * 获取令牌有效期剩余时长（单位：秒）
     */
    public function getExpireTime(Request $request)
    {
        $expireTime = JwtToken::getTokenExp();
        return $this->success('ok', ['expire_time' => $expireTime]);
    }

    /**
     * 令牌清理
     */
    public function clearToken(Request $request)
    {
        JwtToken::clear();
        return $this->success('ok');
    }


    /**
     * 自定义终端client 自定义访问令牌和刷新令牌过期时间
     */
    public function customClient(Request $request)
    {
        // 生成WEB令牌
        $user = [
            'id'  => 2022,
            'name'  => 'Tinywan',
            'client' => JwtToken::TOKEN_CLIENT_WEB
        ];
        $token = JwtToken::generateToken($user);

        // 生成移动端令牌
        $user = [
            'id'  => 2022,
            'name'  => 'Tinywan',
            'client' => JwtToken::TOKEN_CLIENT_MOBILE
        ];
        $token = JwtToken::generateToken($user);

        // 默认是WEB端


        // 自定义访问令牌和刷新令牌过期时间
        $extend = [
            'id'  => 2024,
            'name'  => 'Tinywan',
            'access_exp'  => 600,  // 10 分钟
            'refresh_exp'  => 7200,  // 2 小时
        ];
        $token = JwtToken::generateToken($extend);

        return $this->success('ok', ['token' => $token]);
    }
}
